//
//  ZZWelComeController.swift
//  SinaWeibo
//
//  Created by QUAN on 15/11/13.
//  Copyright © 2015年 QUAN. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class ZZWelComeController: UIViewController {

    override func loadView() {
        view = backImage
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //添加控件
        setUpUi()
        
    }
    
    //开始动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    //开始动画效果
    private func startAnimation() {
        
        iconImage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-self.view.bounds.height + 180)
        }
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    
                    self.welcomeLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        print("OK")
                        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
                })
        }
        
    }
    
    //添加控件
    private func setUpUi() {
        
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        
        //头像约束
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        //文案约束
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        iconImage.sd_setImageWithURL((ZZUserAccountViewModel().headURl), placeholderImage: UIImage(named: "avatar_default_big"))
        
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
        
    }

    //背景图
    private lazy var backImage = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private var iconImage = UIImageView()
    
    //文案
    private var welcomeLabel : UILabel = UILabel(title: (ZZUserAccountViewModel().userName ?? "") + " 欢迎回来", size: 19, color: UIColor.lightGrayColor(), alphaNum: 0)
    

}
